using System;
using System.Windows.Threading;

public class ThreadTools
{

#if SILVERLIGHT
	public enum DispatcherPriority
	{
		Background = 4,
		Normal = 9,
		Send = 10,
	}
#endif

    public static void RunInDispatcher(Dispatcher dispatcher, Action action)
    {
        RunInDispatcher(dispatcher, DispatcherPriority.Normal, action);
    }

    public static void RunInDispatcher(Dispatcher dispatcher, DispatcherPriority priority, Action action)
    {
        if (action == null) { return; }

        if (dispatcher.CheckAccess())
        {
            // we are already on thread associated with the dispatcher -> just call action
            action();
        }
        else
        {
            // we are on different thread, invoke action on dispatcher's thread
            dispatcher.BeginInvoke(
#if !SILVERLIGHT
                priority,
#endif
                action
            );
        }
    }
}
